package chat.components;

import java.awt.Color;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JScrollPane;

import network.ChatMessage;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import config.LauncherConfig;

import app.AppGlobals;

import chat.ChatHandler;
import chat.CommandType;
import chat.MessageType;

@SuppressWarnings("serial")
public class ChatWindow extends JPanel {
	
	public String Name;
	public boolean isSystemWindow = false;
	public boolean isPrivateWindow = false;
	JTextPane chatText = new JTextPane();
	public ChatWindow(String name){
		this.Name = name;
		setLayout(null);
		chatText.setEditable(false);
		chatText.setBounds(0, 0, 740, 370);
		JScrollPane scrollPane = new JScrollPane(chatText);
		scrollPane.setBounds(chatText.getBounds());
		add(scrollPane);
	}
	public ChatWindow(String name,boolean isPrivate, boolean isSystem){
		this.Name = name;
		this.isPrivateWindow = isPrivate;
		this.isSystemWindow = isSystem;
		setLayout(null);
		chatText.setEditable(false);
		chatText.setBounds(0, 0, 740, 370);
		JScrollPane scrollPane = new JScrollPane(chatText);
		scrollPane.setBounds(chatText.getBounds());
		add(scrollPane);
	}
	public void writeMessage(ChatMessage message,boolean autoScroll){
			MessageType type = MessageType.fromInt(message.type);
			CommandType command = CommandType.fromInt(message.command);
			LauncherConfig conf = AppGlobals.launcherConfig;
			
            if (command == CommandType.Me){
            	printText(message.message,conf.MeMsgColor.ToColor());
            }else if (type == MessageType.Team && command == CommandType.TeamServerMessage){
            	printText("[",conf.NormalTextColor.ToColor());
            	printText("TeamMessage: ",conf.ServerMsgColor.ToColor());
            	printText("]: " +message.message,Color.BLACK);
            }else if(type == MessageType.Message || type == MessageType.PrivateMessage || type == MessageType.Team){
				if(message.from.rank > 0){
					printText("[",conf.NormalTextColor.ToColor());
					if(message.from.rank == 1 || message.from.rank == 4)
						printText("Dev",conf.DevColor.ToColor());
					else if(message.from.rank == 2 || message.from.rank == 3)
						printText("Mod",conf.ModColor.ToColor());
					else if(message.from.rank == 99)
						printText("Don",conf.DonColor.ToColor());
					printText("]",conf.NormalTextColor.ToColor());
				}
				
				printText("[",conf.NormalTextColor.ToColor());
				printText(message.from.username,new Color(message.from.r,message.from.g,message.from.b));
				printText("]: " +  message.message,conf.NormalTextColor.ToColor());
			}
			else{
				printText("[",conf.NormalTextColor.ToColor());
				printText(MessageType.fromInt(message.type).toString() ,conf.ServerMsgColor.ToColor());
				printText("]: " +message.message,conf.NormalTextColor.ToColor());
			}				
			
			printText("\n",Color.BLACK);
			if(autoScroll)
				chatText.setCaretPosition(chatText.getStyledDocument().getLength());
	}
	
	MessagePart[] parseText(String text){
		String changedText = text;
		ArrayList<MessagePart> parts = new ArrayList<MessagePart>();
		while (changedText.indexOf("<") >= 0){
			int tagStart = changedText.indexOf("<");
			if(tagStart == -1)
				break;
			int tagEnd = changedText.indexOf(">");
			if(tagEnd == -1)
				break;
			// get value between tags to check if its valid
			if(tagEnd < tagStart)
				break;
			String tabInfo = changedText.substring(tagStart + 1, tagEnd);
			System.out.println(tabInfo);
			break;
			
			//work out tag type
			
			//find the closing tag
			
			//add text before tag as normal text
			
			//add modifyed text
			
			//remove everything before and after the end tag and loop again
				
		}
		if(changedText.length() != 0)
			parts.add(new MessagePart(changedText,Color.BLACK));
		return (MessagePart[])parts.toArray();
	}
	
	private void printText(String text,Color color){
		try {
			
			StyledDocument doc = chatText.getStyledDocument();
			SimpleAttributeSet attr = new SimpleAttributeSet();
			StyleConstants.setForeground(attr, color);
			doc.insertString(doc.getLength(), text, attr);
			
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void writeSystemMessage(String message,boolean autoScroll){
		writeMessage(new ChatMessage(message,null,MessageType.System.getValue(),CommandType.None.getValue()),ChatHandler.autoScroll);
	}
}
